<?php
/**
 *
 *  2Moons Anonce (Market) ... MOD by Kret33n@gmail.com (xnova.cz)
 *
 * for v.1.7
 *
 */

class ShowAnoncePage extends AbstractPage {

	public static $requireModule = 0;

	function __construct() {
		parent::__construct();
	}

	function show() {
		$this->display("page.anonce.default.tpl");
	}


	function res() {
		global $USER, $PLANET, $LNG, $CONF, $LANG, $UNI, $resource;
		define('ANONCE', DB_PREFIX . 'anonce');
		$bid	= HTTP::_GP('bid', 0);
		$uid	= HTTP::_GP('u', 0);
		$mod	= HTTP::_GP('mod', 0);
		$an		= HTTP::_GP('an', 0);
		switch($mod) {	
			case 1:
				switch($an) {
					case 1:
						// delete
						$ANONCC = $GLOBALS['DATABASE']->query("SELECT `metala`, `cristala`, `deuta` FROM ".ANONCE." WHERE `id`='".$bid."';");
			             while ($AdminRoww = $GLOBALS['DATABASE']->fetch_array($ANONCC)) {
//								$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET `metal` = `metal` + '".$AdminRoww['metala']."', `crystal` = `crystal` + '". $AdminRoww['cristala'] ."', `deuterium` = `deuterium` + '". $AdminRoww['deuta'] ."' WHERE `universe` ='".$PLANET['universe']."' AND `galaxy`='".$AdminRoww['galaxie']."' AND `system`='".$AdminRoww['systeme']."' AND `planet`='".$AdminRoww['planete']."'");
							$PLANET[$resource[901]] += $AdminRoww['metala'];
							$PLANET[$resource[902]] += $AdminRoww['cristala'];
							$PLANET[$resource[903]] += $AdminRoww['deuta'];
							$GLOBALS['DATABASE']->query("DELETE FROM ".ANONCE." WHERE `id`='" . $bid . "';"); 
							$this->printMessage(''.$LNG['tech'][901].': <font color=lime>'. $AdminRoww['metala'] .'</font> '.$LNG['tech'][902].':  <font color=lime>'. $AdminRoww['cristala'] .'</font> '.$LNG['tech'][903].':<font color=lime> '. $AdminRoww['deuta'].'</font> ', true, array('game.php?page=anonce&mode=res', 3));
						}
						$GLOBALS['DATABASE']->free_result($ANONCC);
						$this->redirectTo('game.php?page=anonce&mode=res');
//						$this->display("page.anonce.res.tpl");
					break;
					case 2:				
						$ANONCB = $GLOBALS['DATABASE']->query("SELECT `id`, `sender`, `user`, `universe`, `galaxie`, `systeme`, `planete`, `planetid`, `metala`, `cristala`, `deuta`, `metals`, `cristals`, `deuts`, `type` FROM ".ANONCE." WHERE `id`='".$bid."';");
						while ($AdminRowbb = $GLOBALS['DATABASE']->fetch_array($ANONCB)) {
							if($AdminRowbb['metals'] > $PLANET[$resource[901]]) {
								$this->printMessage($LNG['tr_not_enought_metal'], true, array('game.php?page=anonce&mode=res', 3));
							} elseif($AdminRowbb['cristals'] > $PLANET[$resource[902]]) {
		                         $this->printMessage($LNG['tr_not_enought_crystal'], true, array('game.php?page=anonce&mode=res', 3));
							} elseif($AdminRowbb['deuts'] > $PLANET[$resource[903]]) {
		                         $this->printMessage($LNG['tr_not_enought_deuterium'], true, array('game.php?page=anonce&mode=res', 3));
							} else {
//								$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET `metal` = `metal` + '".$AdminRowbb['metala']."', `crystal` = `crystal` + '".$AdminRowbb['cristala']."', `deuterium` = `deuterium` + '".$AdminRowbb['deuta']."' WHERE `universe` ='".$PLANET['universe']."' AND `id` = '". $PLANET['id'] ."'");
//								$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET `metal` = `metal` - '".$AdminRowbb['metals']."', `crystal` = `crystal` - '".$AdminRowbb['cristals']."', `deuterium` = `deuterium` - '".$AdminRowbb['deuts']."' WHERE `universe` ='".$PLANET['universe']."' AND `id` = '". $PLANET['id'] ."'");
								$PLANET[$resource[901]] += $AdminRowbb['metala'];
								$PLANET[$resource[902]] += $AdminRowbb['cristala'];
								$PLANET[$resource[903]] += $AdminRowbb['deuta'];
								$PLANET[$resource[901]] -= $AdminRowbb['metals'];
								$PLANET[$resource[902]] -= $AdminRowbb['cristals'];
								$PLANET[$resource[903]] -= $AdminRowbb['deuts'];
//								$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET `metal` = `metal` + '".$AdminRowbb['metals']."', `crystal` = `crystal` + '".$AdminRowbb['cristals']."', `deuterium` = `deuterium` + '".$AdminRowbb['deuts']."' WHERE `universe` ='".$AdminRowbb['universe']."' AND `galaxy`='".$AdminRowbb['galaxie']."' AND `system`='".$AdminRowbb['systeme']."' AND `planet`='".$AdminRowbb['planete']."'");
								$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET `metal` = `metal` + '" . $AdminRowbb['metals'] . "', `crystal` = `crystal` + '" . $AdminRowbb['cristals'] . "', `deuterium` = `deuterium` + '" . $AdminRowbb['deuts'] . "' WHERE `planet`='" . $AdminRowbb['planetid'] . "';");
								$from 		= $LNG['an_anres'];
								$Subject 	= $LNG['an_anres'];
								$NewUser	= $AdminRowbb['sender'];
								$Time		= TIMESTAMP;
								$message 	= sprintf($LNG['an_mes'], $AdminRowbb['id'] , $AdminRowbb['metala'] , $AdminRowbb['cristala'] , $AdminRowbb['deuta'] , $AdminRowbb['metals'] , $AdminRowbb['cristals'] , $AdminRowbb['deuts']);
								SendSimpleMessage($NewUser, 1, $Time, 1, $from, $Subject, $message);
								$GLOBALS['DATABASE']->query("DELETE FROM ".ANONCE."  WHERE `id`='".$bid."';");
								$this->printMessage(''.$LNG['tech'][901].': <font color=lime>'. $AdminRowbb['metala'] .'</font>  '.$LNG['tech'][902].':  <font color=lime>'. $AdminRowbb['cristala'] .'</font>  '.$LNG['tech'][903].':<font color=lime> '. $AdminRowbb['deuta'].'</font> ', true, array('game.php?page=anonce&mode=res', 3));
							}
						}
						$GLOBALS['DATABASE']->free_result($ANONCB);
						$this->redirectTo('game.php?page=anonce&mode=res');
					break;
				}
			break;
	        default:			
				$PlanetRess = new ResourceUpdate();
				$PlanetRess->CalcResource();
				$PlanetRess->SavePlanetToDB();
				$ANONC 	= $GLOBALS['DATABASE']->query("SELECT `id`, `user`, `sender`, `type`, `universe`, `galaxie`, `systeme`, `planete`, `planetid`, `metala`, `cristala`, `deuta`, `metals`, `cristals`, `deuts` FROM ".ANONCE." WHERE `sender` != '".$USER['id']."' ORDER BY `type`;");
				$SellRes	= array(); 
				while($AdminRow = $GLOBALS['DATABASE']->fetch_array($ANONC)) {	
					$SellRes[]	= array(
			                        'id'		=> $AdminRow['id'],		
			                        'user'		=> $AdminRow['user'],
									'type'		=> $AdminRow['type'],
			                        'universe'	=> $AdminRow['universe'],
			                        'metala'	=> $AdminRow['metala'],
			                        'cristala'	=> $AdminRow['cristala'],
			                        'deuta'		=> $AdminRow['deuta'],
			                        'metals'	=> $AdminRow['metals'],
			                        'cristals'	=> $AdminRow['cristals'],
			                        'deuts'		=> $AdminRow['deuts'],
			                        'galaxy'	=> $AdminRow['galaxie'],
			                        'system'	=> $AdminRow['systeme'],
			                        'planet'	=> $AdminRow['planete'],
			                        'planetid'	=> $AdminRow['planetid'],
			                        'users'		=> $USER['username'],
									'times'		=> $AdminRow['type'] + 86400, // time lot 86400=1 day
									'timend'	=> $AdminRow['type'] + 86400 - TIMESTAMP,
		                            'timelot'	=> TIMESTAMP,
		         	);
					// delete old:
					$times = $AdminRow['type'] + 86400; // time lot  86400=1 day
					$timelot = TIMESTAMP;
					$NewUser = $AdminRow['sender'];
					$bid2 = $AdminRow['id'];
					if ($timelot >= $times && $NewUser == $USER['id']) {
						if ($PLANET['id'] != $AdminRow['planetid']) {
							$GLOBALS['DATABASE']->query("UPDATE " . PLANETS . " SET `metal` = `metal` + '" . $AdminRow['metala'] . "', `crystal` = `crystal` + '" . $AdminRow['cristala'] . "', `deuterium` = `deuterium` + '" . $AdminRow['deuta'] ."' WHERE `id` = '" . $AdminRow['planetid'] . "';");
						}else{
							$PLANET[$resource[901]] += $AdminRow['metala'];
							$PLANET[$resource[902]] += $AdminRow['cristala'];
							$PLANET[$resource[903]] += $AdminRow['deuta'];
						}
						$GLOBALS['DATABASE']->query("DELETE FROM ".ANONCE." WHERE `id`= '".$bid2."';");
						$from 		= $LNG['an_anres'];
						$Subject 	= $LNG['an_anres'];
						$message 	= sprintf($LNG['an_mesexit']);
						SendSimpleMessage($NewUser, 1, $times, 1, $from, $Subject, $message);						
					}
/*					if ($timelot >= $times) {
						$NewUser = $AdminRow['sender'];
						$bid2 = $AdminRow['id'];
//						$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET `metal` = `metal` + '".$AdminRow['metala']."', `crystal` = `crystal` + '". $AdminRow['cristala'] ."', `deuterium` = `deuterium` + '". $AdminRow['deuta'] ."' WHERE `universe` ='".$AdminRow['universe']."' AND  `galaxy`='".$AdminRow['galaxie']."' AND `system`='".$AdminRow['systeme']."' AND `planet`='".$AdminRow['planete']."'");
						if ($PLANET['id'] != $AdminRow['planetid']) {
							$GLOBALS['DATABASE']->query("UPDATE " . PLANETS . " SET `metal` = `metal` + '" . $AdminRow['metala'] . "', `crystal` = `crystal` + '" . $AdminRow['cristala'] . "', `deuterium` = `deuterium` + '" . $AdminRow['deuta'] ."' WHERE `id` = '" . $AdminRow['planetid'] . "';");
						}else{
							$PLANET[$resource[901]] += $AdminRow['metala'];
							$PLANET[$resource[902]] += $AdminRow['cristala'];
							$PLANET[$resource[903]] += $AdminRow['deuta'];
						}
						$GLOBALS['DATABASE']->query("DELETE FROM ".ANONCE." WHERE `id`= '".$bid2."';");
						$from 		= $LNG['an_anres'];
						$Subject 	= $LNG['an_anres'];
						$message 	= sprintf($LNG['an_mesexit']);
						SendSimpleMessage($NewUser, 1, $times, 1, $from, $Subject, $message);						
					}*/
				}			   
				$GLOBALS['DATABASE']->free_result($ANONC);
				$univer = $PLANET['universe'];
				$this->tplObj->assign_vars(array(
					'SellRes'		=> $SellRes,
					'univer'        => $univer,
				));			
				$this->display("page.anonce.res.tpl");
			break;	    
		}
	}






	function resid() {	
		global $USER, $PLANET, $LNG, $UNI, $resource;	
		define('ANONCE', DB_PREFIX . 'anonce');
//		$mod               = $_POST['mod'];
		$met				= $PLANET['metal'];
		$kris				= $PLANET['crystal'];
		$deyt				= $PLANET['deuterium'];	
		$bid		        = HTTP::_GP('bid', 0);
		$uid		        = HTTP::_GP('u',0);
		$mod		        = HTTP::_GP('mod', 0);
		$an			        = HTTP::_GP('an', 0);
		switch($mod) {	
			case 1:
				switch($an) {
					case 1:
						$ANONCC = $GLOBALS['DATABASE']->query("SELECT `metala`, `cristala`, `deuta` FROM ".ANONCE." WHERE `id`='".$bid."';");
						while ($AdminRoww = $GLOBALS['DATABASE']->fetch_array($ANONCC)) {
							// delete
//							$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET `metal` = `metal` + '".$AdminRoww['metala']."', `crystal` = `crystal` + '". $AdminRoww['cristala'] ."', `deuterium` = `deuterium` + '". $AdminRoww['deuta'] ."' WHERE `universe` ='".$PLANET['universe']."' AND `galaxy`='".$AdminRoww['galaxie']."' AND `system`='".$AdminRoww['systeme']."' AND `planet`='".$AdminRoww['planete']."'");
							$PLANET[$resource[901]] += $AdminRoww['metala'];
							$PLANET[$resource[902]] += $AdminRoww['cristala'];
							$PLANET[$resource[903]] += $AdminRoww['deuta'];
							$GLOBALS['DATABASE']->query("DELETE FROM ".ANONCE."  WHERE `id`='".$bid."';"); 
							$this->printMessage(''.$LNG['tech'][901].': <font color=lime>'. $AdminRoww['metala'] .'</font> '.$LNG['tech'][902].':  <font color=lime>'. $AdminRoww['cristala'] .'</font> '.$LNG['tech'][903].':<font color=lime> '. $AdminRoww['deuta'].'</font> ', true, array('game.php?page=anonce&mode=resid', 3));
						 
						}						
						$GLOBALS['DATABASE']->free_result($ANONCC);
						$this->redirectTo('game.php?page=anonce&mode=resid');
//						$this->display("page.anonce.res.tpl");
					break;							
				}
			break;
			default:
				$PlanetRess = new ResourceUpdate();
		        $PlanetRess->CalcResource();
		        $PlanetRess->SavePlanetToDB();
			    $ANONC 	= $GLOBALS['DATABASE']->query("SELECT `id`, `sender`, `user`, `universe`, `galaxie`, `systeme`, `planete`, `metala`, `cristala`, `deuta`, `metals`, `cristals`, `deuts`, `type` FROM ".ANONCE." WHERE `sender`='".$USER['id']."' ORDER BY `type`;");
				$SellRes	= array();
				while ($AdminRow = $GLOBALS['DATABASE']->fetch_array($ANONC)) {	
					if ($USER['id'] == $AdminRow['sender']) {
						$SellRes[]	= array(
			                        'id'		=> $AdminRow['id'],		
			                        'user'		=> $AdminRow['user'],
			                        'universe'	=> $AdminRow['universe'],
			                        'metala'	=> $AdminRow['metala'],
			                        'cristala'	=> $AdminRow['cristala'],
			                        'deuta'		=> $AdminRow['deuta'],
			                        'metals'	=> $AdminRow['metals'],
			                        'cristals'	=> $AdminRow['cristals'],
			                        'deuts'		=> $AdminRow['deuts'],
			                        'galaxy'	=> $AdminRow['galaxie'],
			                        'system'	=> $AdminRow['systeme'],
			                        'planet'	=> $AdminRow['planete'],
			                        'users'		=> $USER['username'],
//			                        'buddyid'	=> $AdminRow['id'],
//			                        'idlot'		=> $AdminRow['id'],
									'times'		=> $AdminRow['type'] + 86400, // time lot 86400=1 day
									'timend'	=> $AdminRow['type'] + 86400 - TIMESTAMP,
		                            'timelot'	=> TIMESTAMP,
						);
					}
					// delete old:
					$times = $AdminRow['type'] + 86400; // time lot  86400=1 day
					$timelot = TIMESTAMP;
					$NewUser = $AdminRow['sender'];
					$bid2 = $AdminRow['id'];
					if ($timelot >= $times && $NewUser == $USER['id']) {
						if ($PLANET['id'] != $AdminRow['planetid']) {
							$GLOBALS['DATABASE']->query("UPDATE " . PLANETS . " SET `metal` = `metal` + '" . $AdminRow['metala'] . "', `crystal` = `crystal` + '" . $AdminRow['cristala'] . "', `deuterium` = `deuterium` + '" . $AdminRow['deuta'] ."' WHERE `id` = '" . $AdminRow['planetid'] . "';");
						}else{
							$PLANET[$resource[901]] += $AdminRow['metala'];
							$PLANET[$resource[902]] += $AdminRow['cristala'];
							$PLANET[$resource[903]] += $AdminRow['deuta'];
						}
						$GLOBALS['DATABASE']->query("DELETE FROM ".ANONCE." WHERE `id`= '".$bid2."';");
						$from 		= $LNG['an_anres'];
						$Subject 	= $LNG['an_anres'];
						$message 	= sprintf($LNG['an_mesexit']);
						SendSimpleMessage($NewUser, 1, $times, 1, $from, $Subject, $message);						
					}
				}
				$GLOBALS['DATABASE']->free_result($ANONC);
				$univer    = $PLANET['universe'];
				$this->tplObj->assign_vars(array(
					'SellRes'		=> $SellRes,
					'univer'        => $univer,
				));		
				$this->display("page.anonce.resid.tpl");
			break;
		}
	}






	function sellres() {	
		global $LNG, $ProdGrid, $resource, $reslist, $CONF, $ExtraDM, $UNI, $USER, $PLANET, $LANG;
		define('ANONCE', DB_PREFIX . 'anonce');
		$LANG->includeLang(array('TECH'));
		$PlanetRess = new ResourceUpdate();
		$PlanetRess->CalcResource();
		$PlanetRess->SavePlanetToDB();
		$aid	= HTTP::_GP('aid', 0);	
		$mod	= HTTP::_GP('mod', '');
		$deu	= $PLANET[$resource[901]];
		$met	= $PLANET[$resource[902]];
		$kry	= $PLANET[$resource[903]];
		if ($mod == 'sr') {		
			$metallin 				= $_POST['metallin'];
			$kristallin				= $_POST['kristallin'];
			$deyterin				= $_POST['deyterin'];
			$metallout 				= $_POST['metallout'];
			$kristallout			= $_POST['kristallout'];
			$deyterout				= $_POST['deyterout'];
			if($metallin < '0') {
				$this->printMessage($LNG['an_cheat'], true, array('game.php?page=logout', 3));
			}elseif($kristallin < '0') {
				$this->printMessage($LNG['an_cheat'], true, array('game.php?page=logout', 3));
			}elseif($deyterin < '0') {
				$this->printMessage($LNG['an_cheat'], true, array('game.php?page=logout', 3));
			}elseif($metallin == '0' && $kristallin == '0' && $deyterin == '0') {
				$this->printMessage($LNG['an_nonuulres'], true, array('game.php?page=anonce&mode=sellres', 3));
			}elseif($metallin == $metallout && $kristallin == $kristallout && $deyterin == $deyterout) {
				$this->printMessage($LNG['an_not'], true, array('game.php?page=anonce&mode=sellres', 3));
			}elseif($metallout == '0' && $kristallout == '0' && $deyterout == '0') {
				$this->printMessage($LNG['an_nuulres'], true, array('game.php?page=anonce&mode=sellres', 3));
			}elseif($metallin > $PLANET[$resource[901]]) {
				$this->printMessage($LNG['an_notres'], true, array('game.php?page=anonce&mode=sellres', 3));
			}elseif($kristallin > $PLANET[$resource[902]]) {
				$this->printMessage($LNG['an_notres'], true, array('game.php?page=anonce&mode=sellres', 3));
			}elseif($deyterin > $PLANET[$resource[903]]) {
				$this->printMessage($LNG['an_notres'], true, array('game.php?page=anonce&mode=sellres', 3));
			}else{
/*				$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET `metal` = `metal` - '". $metallin ."',
                                           `crystal` = `crystal` - '". $kristallin ."',
										   `deuterium` = `deuterium` - '". $deyterin ."'
										   WHERE `universe` ='".$PLANET['universe']."'
										   AND `id` = '". $PLANET['id'] ."'"); */
				$PLANET[$resource[901]] -= $metallin;
				$PLANET[$resource[902]] -= $kristallin;
				$PLANET[$resource[903]] -= $deyterin;
				$GLOBALS['DATABASE']->query("INSERT INTO ".ANONCE." SET `sender` = '".$USER['id']."',
		                                       `user` = '".$USER['username']."',
											   `type` = '".TIMESTAMP. "',
		                                       `metala` = `metala` + '". $metallin ."',
											   `metals` = `metals` + '". $metallout ."',
											   `cristala` = `cristala` + '". $kristallin ."',
											   `cristals` = `cristals` + '". $kristallout ."',
											   `deuta` = `deuta` + '". $deyterin ."',
											   `deuts` = `deuts` + '". $deyterout ."',
                                               `universe` ='".$PLANET['universe']."',
											   `galaxie` ='".$PLANET['galaxy']."',
											   `systeme` ='".$PLANET['system']."',
											   `planete` ='".$PLANET['planet']."',
											   `planetid` ='".$PLANET['id']."';");		
				$this->printMessage(''.$LNG['tech'][901].': <font color=lime>'. $metallin .'</font> '.$LNG['tech'][902].':  <font color=lime>'. $kristallin .'</font> '.$LNG['tech'][903].':<font color=lime> '. $deyterin.'</font> ', true, array('game.php?page=anonce&mode=resid', 3));
			}		
		}
		$this->display("page.anonce.sellres.tpl");
	}



}
?>